import { store } from '@/store'
import { defineStore } from 'pinia'
import { getAccessToken, removeToken } from '@/utils/auth'
import { CACHE_KEY, useCache, deleteUserCache } from '@/hooks/web/useCache'
import { getInfo, loginOut } from '@/api/login'

const { wsCache } = useCache()

interface UserVO {
  uid: string
  name: string
}

interface UserInfoVO {
  // USER 缓存
  permissions: string[]
  roles: string[]
  isSetUser: boolean
  user: UserVO
}

export const useUserStore = defineStore('admin-user', {
  state: (): UserInfoVO => ({
    permissions: [],
    roles: [],
    isSetUser: false,
    user: {
      uid: '',
      name: ''
    }
  }),
  getters: {
    getPermissions(): string[] {
      return this.permissions
    },
    getRoles(): string[] {
      return this.roles
    },
    getIsSetUser(): boolean {
      return this.isSetUser
    },
    getUser(): UserVO {
      return this.user
    }
  },
  actions: {
    async setIsSetUserAction(isSetUser: boolean) {
      this.isSetUser = isSetUser
    },
    async afterLoginAction(user: UserVO & { authorizations: string[] }) {
      this.permissions = user.authorizations
      this.user = user
      let userInfo = wsCache.get(CACHE_KEY.USER)
      if (userInfo) {
        userInfo.permissions = user.authorizations
        userInfo.user = user
      } else {
        userInfo = {
          permissions: user.authorizations,
          user
        }
      }
      wsCache.set(CACHE_KEY.USER, userInfo)
    },
    async setUserInfoAction() {
      if (!getAccessToken()) {
        this.resetState()
        return null
      }
      let userInfo = wsCache.get(CACHE_KEY.USER)
      // if (!userInfo) {
      //   userInfo = await getInfo()
      // }
      function deepFormatMenus(menus: any, parentPath?: string) {
        for (let i = 0; i < menus.length; i++) {
          const menu = menus[i]
          menu.component = menu.path
          menu.path = parentPath ? `${parentPath}${menu?.route?.startsWith?.('/') ? menu?.route : '/' + menu?.route}` : menu.route
          menu.keepAlive = true
          menu.visible = menu.kind === 'menu'
          if (menu.children) {
            deepFormatMenus(menu.children, menu.path)
          }
        }
      }
      const menus = await getInfo()
      deepFormatMenus(menus)
      if (userInfo) {
        userInfo.menus = menus

        this.user = userInfo.user
      } else {
        userInfo = { menus }
      }
      // this.permissions = userInfo.permissions
      // this.roles = userInfo.roles
      // this.user = userInfo.user
      this.isSetUser = true
      wsCache.set(CACHE_KEY.USER, userInfo)
      wsCache.set(CACHE_KEY.ROLE_ROUTERS, menus)
    },
    async setUserAvatarAction(avatar: string) {
      const userInfo = wsCache.get(CACHE_KEY.USER)
      // NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
      // this.user.avatar = avatar
      userInfo.user.avatar = avatar
      wsCache.set(CACHE_KEY.USER, userInfo)
    },
    async setUserNicknameAction(nickname: string) {
      const userInfo = wsCache.get(CACHE_KEY.USER)
      // NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
      // this.user.nickname = nickname
      userInfo.user.nickname = nickname
      wsCache.set(CACHE_KEY.USER, userInfo)
    },
    async loginOut() {
      await loginOut()
      removeToken()
      deleteUserCache() // 删除用户缓存
      this.resetState()
    },
    resetState() {
      this.permissions = []
      this.roles = []
      this.isSetUser = false
      this.user = {
        uid: '',
        name: '',
      }
    }
  }
})

export const useUserStoreWithOut = () => {
  return useUserStore(store)
}
